﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Configuration;
using Spider.Ex;
using Spider.Entity;
using System.Text.RegularExpressions;
using System.Collections;
using System.Threading;

namespace Spider.Parser
{
    public abstract class ParserBase
    {
        public virtual void Parser(string uri, string city, ParserConfig config, ref List<EmailInfo> result)
        {
            WebClient Client = new WebClient();
            Proxy(Client);
            List<KeyValuePair<string, string>> list = new List<KeyValuePair<string, string>>();
            try
            {
                byte[] buffer = Client.DownloadData(uri);
                string html = System.Text.Encoding.UTF8.GetString(buffer);
                string content = GetContext(html);
               
                Regex regex = new Regex(AppVar.EmailReg);
                MatchCollection matches = regex.Matches(content);
            
                IEnumerator enu = matches.GetEnumerator();
                while (enu.MoveNext() && enu.Current != null)
                {
                    Match match = (Match)(enu.Current);
                    string temp = match.Value;

                    if (temp.Contains("@"))
                        result.Add(new EmailInfo() { Email = temp, City = city, Type = config.Name });
                    else if (temp.Length >= 5 && temp.Length <= 10)
                        result.Add(new EmailInfo() { Email = temp + "@qq.com", City = city, Type = config.Name });
                }
            }
            catch (Exception e)
            {
                result.Add(new EmailInfo() { Email = e.Message, City = "503", Type = "503" });
            }
        }

        protected virtual string GetContext(string html)
        {
            return html;
        }

        private void Proxy(WebClient client)
        {
            string proxy = ConfigurationSettings.AppSettings["Proxy"];
            if (!string.IsNullOrEmpty(proxy))
            {
                WebProxy webProxy = new WebProxy(proxy.Split(':')[0], proxy.Split(':')[1].TryInt());
                webProxy.UseDefaultCredentials = true;
                client.Proxy = webProxy;
            }
        }
    }
}
